package patrones;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;

import menu.MenuElement;


/**
 * Cascading Menu design pattern.
 * A screen that display menu.
 *
 * @author Ben Hui
 */

public class MenuConLista extends List
{
  Display display = null; // Display instance of current midlet

  public MenuConLista( Display d )
  {
    super("Untitled", List.IMPLICIT);
    this.display = d;
    addCommand( List.SELECT_COMMAND );
  }

  /**
   * Show a menu on screen.
   * the menu parameter must be a menu container, i.e. it has children
   * @param menu the menu to show on screen
   */
  public void showMenu( MenuElement menu )
  {
    deleteAll();

    display.setCurrent( this );

    for (int i=0; i< menu.children.size(); i++ )
    {
      MenuElement item = (MenuElement) menu.children.elementAt( i );
      append( item.text, null );
    }
    this.setTitle( menu.text );

    setCommandListener( menu );

  }

  public Display getDisplay()
  {
    return display;
  }

  public void deleteAll()
  {
    int s = size();
    for ( int i=0; i< s; i++)
        delete(0);
  }
}
